package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult7;
import com.itheima.common.Simple;
import com.itheima.entity.Subjects;
import com.itheima.entity.SubjectsDirectorys;
import com.itheima.entity.SubjectsTags;
import com.itheima.entity.SubjectsTagsDto;
import com.itheima.mapper.SubjectsMapper;
import com.itheima.mapper.SubjectsTagsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.service.SubjectsTagsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cfr
 * @date 2022/1/9
 * @TIME:20:41
 */
@Service
public class SubjectsTagsServiceImpl extends ServiceImpl<SubjectsTagsMapper, SubjectsTags> implements SubjectsTagsService {
    @Autowired
    private SubjectsTagsMapper subjectsTagsMapper;
    @Autowired
    private SubjectsMapper subjectsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String add(SubjectsTags subjectsTags) {
        //增加添加数据
        subjectsTags.setAddDate(new Date());
        subjectsTags.setTotals(0);
        subjectsTags.setState((short) 1);
        subjectsTagsMapper.insert(subjectsTags);
        return "{id: " + subjectsTags.getId() + "}";
    }

    @Override
    public String modify(SubjectsTags subjectsTags) {
        //修改添加数据
        SubjectsTags oldsubjectsTags = subjectsTagsMapper.selectById(subjectsTags.getId());
        //修改标签数量
        //添加改标签目录数量
        Subjects newSubjects = subjectsMapper.selectById(oldsubjectsTags.getSubjectID());
        newSubjects.setTags(newSubjects.getTotals() + 1);
        subjectsMapper.updateById(newSubjects);

        subjectsTagsMapper.updateById(subjectsTags);
        return "{success: true}";
    }

    @Override
    public PageResult7 finfPage(Long page, Long pagesize, String tagName, Integer state, Integer subjectID) {
        //分页查询
        IPage<SubjectsTags> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<SubjectsTags> wrapper = new LambdaQueryWrapper<>();
        if (tagName != null) {
            wrapper.like(SubjectsTags::getTagName, tagName);
        }
        if (null != state) {
            wrapper.eq(SubjectsTags::getState, state);
        }
        if (null != subjectID) {
            wrapper.eq(SubjectsTags::getSubjectID, subjectID);
        }
        IPage<SubjectsTags> subjectsTagsIPage = subjectsTagsMapper.selectPage(p, wrapper);
        String s = JSON.toJSONString(subjectsTagsIPage.getRecords());
        List<SubjectsTagsDto> subjectsTagsDtos = JSON.parseArray(s, SubjectsTagsDto.class);
        for (SubjectsTagsDto subjectsTagsDto : subjectsTagsDtos) {
            subjectsTagsDto.setUsername(userMapper.selectById(subjectsTagsDto.getCreatorID()).getUsername());
            subjectsTagsDto.setSubjectName(subjectsMapper.selectById(subjectsTagsDto.getSubjectID()).getSubjectName());

        }
        //赛值
        PageResult7 pageResult7 = new PageResult7();
        pageResult7.setCounts(subjectsTagsIPage.getTotal());
        pageResult7.setPage(String.valueOf(page));
        pageResult7.setPagesize(String.valueOf(pagesize));
        pageResult7.setPages(p.getPages());
        pageResult7.setItems(subjectsTagsDtos);
        return pageResult7;
    }

    @Override
    public String state(Integer id, Integer state) {
        SubjectsTags subjectsTags = subjectsTagsMapper.selectById(id);
        subjectsTags.setState(state.shortValue());
        //判断禁用更改学科标签数量
        Subjects subjects = subjectsMapper.selectById(subjectsTags.getSubjectID());
        if (state == 0) {
            subjects.setTags(subjects.getTags() - 1);
        }
        if (state == 1) {
            subjects.setTags(subjects.getTags() + 1);
        }
        subjectsMapper.updateById(subjects);

        subjectsTagsMapper.updateById(subjectsTags);
        return "{success: true}";
    }

    @Override
    public String delete(SubjectsTagsDto subjectsTagsDto) {
        //判断禁用更改学科标签数量
        Subjects subjects = subjectsMapper.selectById(subjectsTagsDto.getSubjectID());
        subjects.setTags(subjects.getTags() - 1);
        subjectsMapper.updateById(subjects);
        subjectsTagsMapper.deleteById(subjectsTagsDto.getId());
        return "{success: true}";
    }

    @Override
    public SubjectsTagsDto fingById(Integer id) {
        SubjectsTags subjectsTags = subjectsTagsMapper.selectById(id);
        SubjectsTagsDto subjectsTagsDto = new SubjectsTagsDto();
        BeanUtils.copyProperties(subjectsTags, subjectsTagsDto);
        subjectsTagsDto.setCreator(userMapper.selectById(subjectsTagsDto.getCreatorID()).getUsername());
        return subjectsTagsDto;
    }

    @Override
    public List<Simple> subjectsTagsService(Integer subjectID) {
        ArrayList<Simple> simples = new ArrayList<>();
        LambdaQueryWrapper<SubjectsTags> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubjectsTags::getSubjectID, subjectID);
        List<SubjectsTags> subjectsTags = subjectsTagsMapper.selectList(wrapper);
        for (SubjectsTags tags : subjectsTags) {
            Simple simple = new Simple();
            simple.setLabel(tags.getTagName());
            simple.setValue(tags.getId());
            simples.add(simple);
        }
        return simples;
    }

}
